package com.hc.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hc.util.IdWorker;
import com.hc.util.LoggerUtil;

@Controller
@RequestMapping(value = "File")
public class FileUtilsController extends BaseController {
	@RequestMapping("/upload")
	@ResponseBody
	public JSON uploadFiles(HttpServletRequest req, HttpServletResponse resp,@RequestParam("files") CommonsMultipartFile[] files, String title,
			String respWay) {
		try {
			JSONArray arr = new JSONArray();
			for (MultipartFile file : files) {
				// 此处MultipartFile[]表明是多文件,如果是单文件MultipartFile就行了
				if (file.isEmpty()) {
					System.out.println("文件未上传!");
				} else {
					String key = IdWorker.createId();
					// 查看文件上传路径,方便查找
					// 把文件上传至path的路径
					File localFile = new File(FILE_PATH + key);
					file.transferTo(localFile);
					arr.add(key);
				}
			}
			return Message(SUCCESS, "操作成功", arr);
		} catch (Exception e) {
			return LoggerUtil.logInfo(e, "服务器异常,请稍后再试");
		}
	}
}
